import 'package:aichat_flutter_wallet/module_common/widgets/button/public_button.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/header/page_header.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/log/log.dart';

import '../assets_wallet_item.dart';

class SelectWalletListPage extends StatefulWidget {
  const SelectWalletListPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _SelectWalletListPageState();
  }
}

class _SelectWalletListPageState extends State<SelectWalletListPage> {
  bool sortMode = false;
  late List<Wallet> _walletList;

  @override
  void initState() {
    super.initState();
    _walletList = ObjectBox().walletModel.getSortedWalletList();
  }

  Widget _walletItemBuilder(BuildContext context, int index) {
    Wallet wallet = _walletList[index];
    Log.logger.d(wallet);
    return WalletListItem(
      key: sortMode ? Key(wallet.id.toString()) : null,
      wallet: wallet,
      sortMode: sortMode,
      topCallback: sortMode
          ? () {
              setState(() {
                _walletList.remove(wallet);
                _walletList.insert(0, wallet);
              });
            }
          : null,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: PageHeaderCom(
        title: 'Select Wallet',
      ),
      body: Column(
        children: [
          Expanded(
              child: ListView.builder(
            itemCount: _walletList.length,
            itemBuilder: _walletItemBuilder,
          )),
          // const HorizontalLine(),
        ],
      ),
      bottomNavigationBar: Padding(
        padding: const EdgeInsets.only(left: 0, right: 0, bottom: 10, top: 10),
        child: PublicButton(
          text: 'Add Wallet',
          onClick: () {
            // Navigator.of(context).popAndPushNamed(Routers.createOrImportWallet);
          },
        ),
      ),
    );
  }
}
